/**
 * FileName: CrmExportServiceImpl
 * Author:   秋水浮萍
 * Date:     2020/10/3 21:36
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.crm.crm.service.impl;

import com.crm.crm.controller.CrmTypeController;
import com.crm.crm.mapper.CrmExportMapper;
import com.crm.crm.mapper.CrmPartnerMapper;
import com.crm.crm.service.CrmExportService;
import com.crm.entity.Partner;
import com.crm.model.FundStructModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author 秋水浮萍
 * @create 2020/10/3
 * @since 1.0.0
 */
@Service
public class CrmExportServiceImpl implements CrmExportService {

    @Autowired
    private CrmPartnerMapper mapper;
    @Autowired
    private CrmExportMapper selectMapper;

    private String returns(String type, String value) {
        return CrmTypeController.crmMap.get(type).get(value);
    }

    private void partnerS(List<Partner> list) {
        for (Partner p : list
        ) {
            if (p.getSource() != null) {
                p.setSource(CrmTypeController.crmMap.get("source").get(p.getSource()));
            } else {
                p.setSource("");
            }
            if (p.getExitType() != null) {
                p.setExitType(returns("exitTypeList", p.getExitType()));
            }
            if (p.getOrgType() != null) {
                String[] orgTypes = new String[p.getOrgType().length];
                for (int i = 0; i < p.getOrgType().length; i++) {
                    if (i != p.getOrgType().length - 1) {
                        orgTypes[i] = CrmTypeController.crmMap.get("orgTypeList").get(p.getOrgType()[i]) + ",";
                    } else {
                        orgTypes[i] = CrmTypeController.crmMap.get("orgTypeList").get(p.getOrgType()[i]);
                    }
                }
                p.setOrgType(orgTypes);
            }
            if (p.getOverArea() != null) {
                String[][] a = new String[p.getOverArea().length][];
                for (int i = 0; i < p.getOverArea().length; i++) {
                    String[] s = new String[p.getOverArea()[i].length];
                    for (int j = 0; j < p.getOverArea()[i].length; j++) {
                        s[j] = CrmTypeController.citys.get(p.getOverArea()[i][j]);
                    }
                    a[i] = s;
                }
                p.setOverArea(a);
            }

        }
    }

    @Override
    public List<Partner> exportAssetInf(String createId, String company, Integer partnerType, String belong, String[] cities, String orgType, String orgRemark, String typeId) {
        List<Partner> exportAssetInf = mapper.findAssetInf(createId, company, partnerType, belong, cities, orgType, orgRemark, typeId);
        for (Partner p : exportAssetInf
        ) {
            if (p.getSource() != null) {
                p.setSource(CrmTypeController.crmMap.get("source").get(p.getSource()));
            } else {
                p.setSource("");
            }
            if (p.getOrgType() != null) {
                String[] orgTypes = new String[p.getOrgType().length];
                for (int i = 0; i < p.getOrgType().length; i++) {
                    if (i != p.getOrgType().length - 1) {
                        orgTypes[i] = CrmTypeController.crmMap.get("orgTypeList").get(p.getOrgType()[i]) + ",";
                    } else {
                        orgTypes[i] = CrmTypeController.crmMap.get("orgTypeList").get(p.getOrgType()[i]);
                    }
                }
                p.setOrgType(orgTypes);
            }
            if (p.getAssetInfo() != null) {
                if (p.getAssetInfo().getBelong() != null) {
                    p.getAssetInfo().setBelong(returns("attr", p.getAssetInfo().getBelong()));
                }
                if (p.getAssetInfo().getBusinessType() != null) {
                    String[] type = new String[p.getAssetInfo().getBusinessType().length];
                    for (int i = 0; i < p.getAssetInfo().getBusinessType().length; i++) {
                        type[i] = returns("bizTypeList", p.getAssetInfo().getBusinessType()[i]);


                    }
                    p.getAssetInfo().setBusinessType(type);
                }
            }
            if (p.getOverArea() != null) {
                String[][] a = new String[p.getOverArea().length][];
                for (int i = 0; i < p.getOverArea().length; i++) {
                    String[] s = new String[p.getOverArea()[i].length];
                    for (int j = 0; j < p.getOverArea()[i].length; j++) {
                        s[j] = CrmTypeController.citys.get(p.getOverArea()[i][j]);
                    }
                    a[i] = s;
                }
                p.setOverArea(a);
            }

        }
        return exportAssetInf;
    }

    @Override
    public List<Partner> exportFundInfo(String createId, String company, Integer partnerType, String orgType, String orgRemark, String currency, String incomeType, String struct, Integer scaleMin, Integer scaleMax, String deadline, String[] cities, String typeId, String typeName, Integer incomeRate) {
        List<Partner> exportFundInfo = mapper.findFundInfo(createId, company, partnerType, orgType, orgRemark, currency, incomeType, struct, scaleMin, scaleMax, deadline, cities, typeId, typeName, incomeRate);
        partnerS(exportFundInfo);
        for (Partner p : exportFundInfo
        ) {
            if (p.getFundInfo() != null) {
                if (p.getFundInfo().getStructList() != null) {
                    for (FundStructModel f : p.getFundInfo().getStructList()
                    ) {
                        if (f.getCurrency() != null) f.setCurrency(returns("currencyList", f.getCurrency()));
                        if (f.getStruct() != null) f.setStruct(returns("structList", f.getStruct()));
                        if (f.getScaleMax() != null) f.setScaleMax(returns("abilityList", f.getScaleMax()));
                        if (f.getScaleMin() != null) f.setScaleMin(returns("abilityList", f.getScaleMin()));
                        if (f.getDeadline() != null) f.setDeadline(returns("deadlineList", f.getDeadline()));
                        if (f.getIncomeType() != null) f.setIncomeType(returns("incomeTypeList", f.getIncomeType()));

                    }
                }
                if (p.getFundInfo().getFancySList() != null) {
                    String s[] = new String[p.getFundInfo().getFancySList().length];
                    for (int i = 0; i < p.getFundInfo().getFancySList().length; i++) {
                        s[i] = returns("assetsTypeList", p.getFundInfo().getFancySList()[i]);
                    }
                    p.getFundInfo().setFancySList(s);
                }
            }

        }
        return exportFundInfo;
    }

    @Override
    public List<Partner> exportExitInfo(String createId, String company, Integer partnerType, String orgType, String orgRemark, String usage, String typeId, String typeName, String[] cities, Integer abilityMin, Integer abilityMax) {
        List<Partner> exportExitInfo = selectMapper.findExitInfo(createId, company, partnerType, orgType, orgRemark, usage, typeId, typeName, cities, abilityMin, abilityMax);
        partnerS(exportExitInfo);
        for (Partner p : exportExitInfo
        ) {
            if (p.getExitInfo() != null) {
                if (p.getExitInfo().getUsage() != null) {
                    String[] b = new String[p.getExitInfo().getUsage().length];
                    for (int i = 0; i < p.getExitInfo().getUsage().length; i++) {
                        b[i] = returns("usageList", p.getExitInfo().getUsage()[i]);
                    }
                    p.getExitInfo().setUsage(b);
                }
                if (p.getExitInfo().getAbilityMax() != null)
                    p.getExitInfo().setAbilityMax(returns("abilityList", p.getExitInfo().getAbilityMax()));
                if (p.getExitInfo().getAbilityMin() != null)
                    p.getExitInfo().setAbilityMin(returns("abilityList", p.getExitInfo().getAbilityMin()));
                if (p.getExitInfo().getFancySList() != null) {
                    String s[] = new String[p.getExitInfo().getFancySList().length];
                    for (int i = 0; i < p.getExitInfo().getFancySList().length; i++) {
                        s[i] = returns("assetsTypeList", p.getExitInfo().getFancySList()[i]);
                    }
                    p.getExitInfo().setFancySList(s);
                }
            }
        }
        return exportExitInfo;
    }

    @Override
    public List<Partner> exportJusticeInfo(String createId, Integer partnerType, String company, String post, String[] cities) {
        List<Partner> exportJusticeInfo = selectMapper.findJusticeInfo(createId, partnerType, company, post, cities);
        partnerS(exportJusticeInfo);
        for (Partner j : exportJusticeInfo
        ) {
            if (j.getJusticeInfo() != null) {
                if (j.getJusticeInfo().getRelative() != null)
                    j.getJusticeInfo().setRelative(returns("relativeList", j.getJusticeInfo().getRelative()));
                if (j.getJusticeInfo().getOrgType() != null)
                    j.getJusticeInfo().setOrgType(returns("orgTypeList", j.getJusticeInfo().getOrgType()));
                if (j.getJusticeInfo().getRelative() != null)
                    j.getJusticeInfo().setRelative(returns("relativeList", j.getJusticeInfo().getRelative()));

                if (j.getJusticeInfo().getRelative() != null)
                    j.getJusticeInfo().setRelative(returns("relativeList", j.getJusticeInfo().getRelative()));
                if (j.getJusticeInfo().getArea() != null) {
                    String[][] a = new String[j.getJusticeInfo().getArea().length][];
                    for (int i = 0; i < j.getJusticeInfo().getArea().length; i++) {
                        String[] s = new String[j.getJusticeInfo().getArea()[i].length];
                        for (int k = 0; k < j.getJusticeInfo().getArea()[i].length; k++) {
                            s[k] = CrmTypeController.citys.get(j.getJusticeInfo().getArea()[i][k]);
                        }
                        a[i] = s;
                    }
                    j.getJusticeInfo().setArea(a);
                }
            }
        }

        return exportJusticeInfo;
    }

    @Override
    public List<Partner> exportOrgInfo(String createId, Integer partnerType, String company, String post, String[] cities) {
        List<Partner> exportOrgInfo = selectMapper.findOrgInfo(createId, partnerType, company, post, cities);
        partnerS(exportOrgInfo);
        for (Partner o : exportOrgInfo
        ) {
            if (o.getOrgInfo() != null) {
                if (o.getOrgInfo().getRelative() != null)
                    o.getOrgInfo().setRelative(returns("relativeList", o.getOrgInfo().getRelative()));
                if (o.getOrgInfo().getOrgType() != null)
                    o.getOrgInfo().setOrgType(returns("orgTypeList", o.getOrgInfo().getOrgType()));
                if (o.getOrgInfo().getRelativeArea() != null) {
                    String[][] a = new String[o.getOrgInfo().getRelativeArea().length][];
                    for (int i = 0; i < o.getOrgInfo().getRelativeArea().length; i++) {
                        String[] s = new String[o.getOrgInfo().getRelativeArea()[i].length];
                        for (int j = 0; j < o.getOrgInfo().getRelativeArea()[i].length; j++) {
                            s[j] = CrmTypeController.citys.get(o.getOrgInfo().getRelativeArea()[i][j]);
                        }
                        a[i] = s;
                    }
                    o.getOrgInfo().setRelativeArea(a);
                }
            }
        }


        return exportOrgInfo;
    }

    @Override
    public List<Partner> exportPeerInfo(String createId, String company, Integer partnerType, String orgType, String bizListId, String coopListId, String[] cities) {
        List<Partner> exportPeerInfo = selectMapper.findPeerInfo(createId, company, partnerType, orgType, bizListId, coopListId, cities);
        partnerS(exportPeerInfo);
        for (Partner p : exportPeerInfo
        ) {
            if (p.getPeerInfo() != null) {
                if (p.getPeerInfo().getBizSList() != null) {
                    String s[] = new String[p.getPeerInfo().getBizSList().length];
                    for (int i = 0; i < p.getPeerInfo().getBizSList().length; i++) {
                        s[i] = returns("peerTypeList", p.getPeerInfo().getBizSList()[i]);
                    }
                    p.getPeerInfo().setBizSList(s);
                }
                if (p.getPeerInfo().getCoopSList() != null) {
                    String s[] = new String[p.getPeerInfo().getCoopSList().length];
                    for (int i = 0; i < p.getPeerInfo().getCoopSList().length; i++) {
                        s[i] = returns("peerBizList", p.getPeerInfo().getCoopSList()[i]);
                    }
                    p.getPeerInfo().setCoopSList(s);
                }
            }
        }

        return exportPeerInfo;
    }


}